var myGrid=undefined;

(function($){
	$.LandRowEditGrid=function(gridId,deleteUrl,addUpdateUrl){
		myGrid=new MyGrid(gridId,deleteUrl,addUpdateUrl);
		return myGrid;
	}
})(jQuery)


var MyGrid=function(gridId,deleteUrl,addUpdateUrl){
	this.gridId=gridId;
	this.editIndex = undefined;
	this.clickIndex= undefined;
	this.deleteUrl=deleteUrl;
	this.addUpdateUrl = addUpdateUrl;
	this.row=undefined;
	this.create={createGrid:function(){createGrid()}}
}



function createGrid(){
	$("#"+myGrid.gridId).datagrid({
		url: currenthost + '/domino/query.ajax',
		title: 'Domino信息',
		iconCls: 'icon-edit',
		pagination: true,
		fitColumns:true,
		pageSize: 10,
		pageList: [10, 20, 30],
		columns: [[{
			field: 'id',
			hidden: true
		},
		{
			field: 'gmtCreated',
			hidden: true
		},
		{
			field: 'name',
			title: '名称',
			width:  100,
			editor:'text'
		},
		{
			field: 'server',
			title: '服务器',
			width:  100,
			editor:{
				type:'validatebox',
				options:{
					required:true
				}
			}
		},
		{
			field: 'port',
			title: '端口',
			width:  50,
			editor:{
				type:'validatebox',
				options:{
					required:true
				}
			}
		},
		{
			field: 'dbPath',
			title: '索引库',
			width:  150,
			editor:{
				type:'validatebox',
				options:{
					required:true
				}
			}
		},
		{
			field: 'userName',
			title: '用户名',
			width:  50,
			editor:{
				type:'validatebox',
				options:{
					required:true
				}
			}
		},
		{
			field: 'password',
			title: '密码',
			width:  50,
			editor:{
				type:'validatebox',
				options:{
					required:true
				}
			}
		},
		{
			field: 'statue',
			title: '状态',
			width:  50,
			editor:{
				type:'validatebox',
				options:{
					required:true,
					validType:'maxLength[1]'
				}
			}
		},
		{
			field: 'searchCondition',
			title: '搜索条件',
			width:  350,
			editor:{
				type:'validatebox',
				options:{
					required:true
				}
			}
		}]],
		toolbar: [{
			text: '增加',
			iconCls: 'icon-add',
			handler: myGrid.append
		},
		'-', {
			text: '删除',
			iconCls: 'icon-remove',
			handler: myGrid.remove
		},
		'-', {
			text: '保存',
			iconCls: 'icon-save',
			handler: function() {
				myGrid.accept();
			}
		},
		'-', {
			text: '取消',
			iconCls: 'icon-undo',
			handler: function() {
				myGrid.reject();
			}
		}],
		onClickRow:myGrid.onClickRow,
		onDblClickRow:myGrid.onDblClickRow
	});
}


MyGrid.prototype.onClickRow=function(index){
	$("#"+myGrid.gridId).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	if (myGrid.editIndex != index){
		if (myGrid.endEditing()){
			myGrid.clickIndex=index;
			$('#'+myGrid.gridId).datagrid('selectRow', index);
			var rows = $("#"+myGrid.gridId).datagrid('getSelections');
			myGrid.row =rows[0];
		} else {
			myGrid.clickIndex = myGrid.editIndex;
			$('#'+myGrid.gridId).datagrid('selectRow', myGrid.editIndex);
		}
	}
}


MyGrid.prototype.endEditing = function(){
	if (myGrid.editIndex == undefined){
		$("#"+myGrid.gridId).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		return true
		}
	if ($('#'+myGrid.gridId).datagrid('validateRow', myGrid.editIndex)){
		$('#'+myGrid.gridId).datagrid('endEdit', myGrid.editIndex);
		myGrid.editIndex = undefined;
		$("#"+myGrid.gridId).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		return true;
	} else {
		return false;
	}
}



MyGrid.prototype.append=function(){
	if (myGrid.endEditing()){
		if(myGrid.row==undefined){
			$('#'+myGrid.gridId).datagrid('appendRow',{});
		}else{
			var row = {};
			deepCopy(row, myGrid.row);
			row.id=null;
			$('#'+myGrid.gridId).datagrid('appendRow',row);
		}
		myGrid.editIndex = $('#'+myGrid.gridId).datagrid('getRows').length-1;
		myGrid.clickIndex = myGrid.editIndex;
		$('#'+myGrid.gridId).datagrid('selectRow', myGrid.editIndex).datagrid('beginEdit', myGrid.editIndex);
	}
}


MyGrid.prototype.remove = function(){
	if (myGrid.clickIndex == undefined){return}
	var rows = $("#"+myGrid.gridId).datagrid('getSelections');
	if(rows[0].id==null){
		$('#'+myGrid.gridId).datagrid('cancelEdit', myGrid.clickIndex).datagrid('deleteRow', myGrid.clickIndex);
		myGrid.clickIndex=undefined;
	}else{
		myGrid.delRow(rows[0].id,myGrid.clickIndex);
	}
}

MyGrid.prototype.accept=function(){
	if (myGrid.endEditing()){
		var rows = myGrid.getChanges();
		if(rows.length==0){
			jQuery.messager.show({
				msg: '没有数据改变！',
				title: '提示'
			});
			return;
		}
			var result = myGrid.insertRows(rows);

	}
}


MyGrid.prototype.insertRows=function(rows){
	var insertResult;
	jQuery.ajax({
		url: myGrid.addUpdateUrl,
		data: {
			rows: JSON.stringify(rows)
		},
		dataType: 'json',
		type:'POST',
		success: function(result) {
			insertResult = result;
			if(result.success){
				$.messager.alert('保存成功',"祝贺你成功保存！");
				$('#'+myGrid.gridId).datagrid('reload', "");
			}else{
				$.messager.alert('保存失败',result.msg,'error');
			}
		}
	});
	return insertResult;
}

MyGrid.prototype.delRow=function(id,index){
	jQuery.messager.confirm('确认', '您是否要删除当前选中的项目？',function(r) {
		if(!r){
			return;
		}
		jQuery.ajax({
			url: myGrid.deleteUrl,
			data: {
				id: id
			},
			dataType: 'json',
			type:'POST',
			success: function(result) {
				delResult = result;
				if(result.success){
					$.messager.alert('删除成功',"祝贺删除成功！");
					$('#'+myGrid.gridId).datagrid('cancelEdit', myGrid.clickIndex).datagrid('deleteRow', myGrid.clickIndex);
					myGrid.clickIndex=null;
					$('#'+myGrid.gridId).datagrid('reload', "");
				}else{
					$.messager.alert('删除失败',result.msg,'error');
				}
			}
		});
	});
}


MyGrid.prototype.onDblClickRow=function (index,rowData){
	$("#"+myGrid.gridId).datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	if (myGrid.editIndex != index){
		if (myGrid.endEditing()){
			$('#'+myGrid.gridId).datagrid('selectRow', index)
					.datagrid('beginEdit', index);
			myGrid.editIndex = index;
		} else {
			$('#'+myGrid.gridId).datagrid('selectRow', myGrid.editIndex);
		}
	}
}

MyGrid.prototype.reject=function(){
	$('#'+myGrid.gridId).datagrid('rejectChanges');
	myGrid.editIndex = undefined;
}

MyGrid.prototype.getChanges=function(){
	var rows = $('#'+myGrid.gridId).datagrid('getChanges');
	return rows;
}



var serializeObject = function(form) {
	var o = {};
	jQuery.each(form.serializeArray(),
	function(index) {
		if (o[this['name']]) {
			o[this['name']] = o[this['name']] + "," + this['value'];
		} else {
			o[this['name']] = this['value'];
		}
	});
	return o;
}







function getType(o)
{
    var _t;
    return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();
}
function deepCopy(destination,source){
    for(var p in source)
      {
        if(getType(source[p])=="array"||getType(source[p])=="object")
        {
            destination[p]=getType(source[p])=="array"?[]:{};
            arguments.callee(destination[p],source[p]);
        }
        else
        {
            destination[p]=source[p];
        }
    }
}




